package com.microservice.salmon.web.controller;

import com.microservice.salmon.service.brand.BrandService;
import com.microservice.salmon.service.vo.BrandVO;
import com.salmon.common.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 王洪庆 2019-03-15 12:35
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @PostMapping
    public ResponseEntity<ApiResponse> saveBrand(@RequestBody BrandVO brandVO) {
        this.brandService.add(brandVO);
        return new ResponseEntity<>(ApiResponse.ofSuccess(), HttpStatus.CREATED);
    }

    @GetMapping
    public ResponseEntity<ApiResponse> getBrand(@RequestBody BrandVO brandVO) {
        return new ResponseEntity<>(ApiResponse.ofSuccess(this.brandService.obtainBrandList(brandVO)),
                HttpStatus.OK);
    }

    @PutMapping
    public ResponseEntity<ApiResponse> updateBrand(@RequestBody BrandVO brandVO) {
        this.brandService.update(brandVO);
        return new ResponseEntity<>(ApiResponse.ofSuccess(), HttpStatus.OK);
    }

    @DeleteMapping
    public ResponseEntity<ApiResponse> deleteBrand(@RequestBody BrandVO brandVO) {
        this.brandService.delete(brandVO.getBrandId());
        return new ResponseEntity<>(ApiResponse.ofSuccess(), HttpStatus.OK);
    }

    @GetMapping("{brandId:\\d+}")
    public ResponseEntity<ApiResponse> getBrand(@PathVariable("brandId") Integer brandId) {
        return new ResponseEntity<>(ApiResponse.ofSuccess(this.brandService.getBrandInfoById(brandId)),
                HttpStatus.OK);
    }

    @GetMapping("/category/{categoryId:\\d+}")
    public ResponseEntity<ApiResponse> getBrandByCategory(@PathVariable("categoryId") Integer categoryId) {
        return ResponseEntity.ok(ApiResponse.ofSuccess(this.brandService.obtainBrandByCategoryId(categoryId)));
    }

}
